package io.ai.arch.java.ai;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("api/ai/prompt")
@RequiredArgsConstructor
public class PromptApi {

    private final ChatModel chatModel;


    /**
     * http://localhost:8888/api/ai/prompt/promptTemplate?adjective=funny&topic=swimming
     * @param adjective
     * @param topic
     * @return
     */

    /*

    {
  "output": {
    "messageType": "ASSISTANT",
    "metadata": {
      "role": "ASSISTANT",
      "messageType": "ASSISTANT",
      "refusal": "",
      "finishReason": "STOP",
      "annotations": [],
      "index": 0,
      "id": "chat-036d3d1ecc4a418ea9c7ad89214c850c"
    },
    "toolCalls": [],
    "media": [],
    "text": "Why did the swimmer bring a ladder to the pool?\n\nBecause he wanted to take the high dive!"
  },
  "metadata": {
    "finishReason": "STOP",
    "contentFilters": [],
    "empty": true
  }
}

     */
    @GetMapping("/promptTemplate")
    public Generation promptTemplate(String adjective, String topic) {


        PromptTemplate promptTemplate = new PromptTemplate("Tell me a {adjective} joke about {topic}");

        Prompt prompt = promptTemplate.create(Map.of("adjective", adjective, "topic", topic));

        return chatModel.call(prompt).getResult();

    }

    /**
     * http://localhost:8888/api/ai/prompt/systemPromptTemplate?name=lins&voice=choplin
     * @param name
     * @param voice
     * @return
     */
    /*
    [
  {
    "output": {
      "messageType": "ASSISTANT",
      "metadata": {
        "role": "ASSISTANT",
        "messageType": "ASSISTANT",
        "refusal": "",
        "finishReason": "STOP",
        "annotations": [],
        "index": 0,
        "id": "chat-db7ac5e580574667ad9f4702e932dd8f"
      },
      "toolCalls": [],
      "media": [],
      "text": "Ahoy there, matey! I be Lins, here to help ye find information about them scurvy sea dogs from the Golden Age of Piracy. Here be three of the most famous ones:\n\n1. Captain William Kidd: A Scottish sailor who turned to piracy in the late 17th century. He was commissioned by the British government to hunt down pirates, but he ended up becoming one himself. He was eventually captured and executed in 1701.\n2. Captain Bartholomew Roberts: A Welsh pirate who operated in the Caribbean and Atlantic Ocean during the early 18th century. He is considered one of the most successful pirates in history, capturing over 400 ships during his career. He was killed in battle in 1722.\n3. Captain Edward Teach, also known as Blackbeard: An English pirate who operated in the Caribbean and Atlantic Ocean during the early 18th century. He was known for his long black beard and his reputation for brutality. He was killed in battle in 1718.\n\nThese pirates, and many others, turned to piracy for various reasons. Some were driven by greed, while others were motivated by a desire for adventure and freedom. However, piracy was a dangerous and often short-lived career, as many pirates were eventually caught and executed by the authorities."
    },
    "metadata": {
      "finishReason": "STOP",
      "contentFilters": [],
      "empty": true
    }
  }
]
     */
    @GetMapping("/systemPromptTemplate")
    public List<Generation> SystemPromptTemplate(String name, String voice) {

        String userText = """
                Tell me about three famous pirates from the Golden Age of Piracy and why they did.
                Write at least a sentence for each pirate.
                """;

        Message userMessage = new UserMessage(userText);

        String systemText = """
                You are a helpful AI assistant that helps people find information.
                Your name is {name}
                You should reply to the user's request with your name , should be use chinese, also in the style of a {voice}.
                """;

        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "voice", voice));

        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
        return chatModel.call(prompt).getResults();
    }
}
